package br.com.verde.exemploteca.recursivity

object Recursivity {
  def mdc(a: Int, b: Int): Int = if (a % b == 0) b else mdc(b, a % b)

  def fatorialRecursive(a: Int): Int = if (a == 1) 1 else a * fatorialRecursive(a - 1)

  def fatorial(n: Int) = {
    def fatorialTailRecursive(current: Int, previous: Int): Int =
      if (previous == 1) current else fatorialTailRecursive(current * previous, previous - 1)

    fatorialTailRecursive(n, n - 1)
  }

  def badFatorial(a: Int) = fatorialRecursive(a)

  def main(args: Array[String]) {
    print(fatorial(5))
    //		print(badFatorial(10000))	// Stack Over Flow
  }

}